package com.example.jfybasefirme;

import androidx.fragment.app.Fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Handler;

import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.example.baselib.base.BaseActivity;
import com.example.baselib.baseurl.GuidUrl;

import com.example.baselib.widght.easynavigation.utils.NavigationUtil;
import com.example.baselib.widght.easynavigation.view.EasyNavigationBar;
import com.example.homepage.fragment.HomePageFragment;
import com.example.jfybasefirme.customview.KickBackAnimator;
import com.example.knowledge.fragement.KnowLedgeFragment;
import com.example.message.fragment.MessageFragment;
import com.example.mine.fragment.MineFragment;
import com.gyf.immersionbar.ImmersionBar;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private EasyNavigationBar navigationBar;

    private String[] tabText = {"首页", "知识", "消息", "我的"};
    //未选中icon
    private int[] normalIcon = {R.mipmap.home_page_default, R.mipmap.know_ledge_default, R.mipmap.messge_default, R.mipmap.mine_default};
    //选中时icon
    private int[] selectIcon = {R.mipmap.home_page_pressed, R.mipmap.know_ledge_pressed, R.mipmap.message_pressed, R.mipmap.mine_pressed};

    private int[] menuIconItems0 = {R.mipmap.health_history_icon, R.mipmap.health_assessment_icon, R.mipmap.health_indicators_icon, R.mipmap.historical_cases_icon};
    private String[] menuTextItems0 = {"健康史", "健康评估", "健康指标", "历史病例"};

    private int[] menuIconItems1 = {R.mipmap.physical_examination_icon, R.mipmap.dietary_advice_icon, R.mipmap.medication_reminder_icon, R.mipmap.second_visit_icon};
    private String[] menuTextItems1 = {"体检报告", "膳食建议", "用药提醒", "复诊提醒"};
    private LinearLayout menuLayout0,menuLayout1;
    private View cancelImageView;
    private List<Fragment> fragments = new ArrayList<>();
    private Handler mHandler = new Handler();

    private boolean flag = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main1;
    }

    @Override
    public void initView() {
        navigationBar = findViewById(R.id.navigationBar);
        if (fragments.size() > 0) {
            return;
        }
        HomePageFragment homeFragment = new HomePageFragment();
        KnowLedgeFragment knowledgeFragment = new KnowLedgeFragment();
        MessageFragment messageFragment = new MessageFragment();
        MineFragment mineFragment = new MineFragment();

        fragments.add(homeFragment);
        fragments.add(knowledgeFragment);
        fragments.add(messageFragment);
        fragments.add(mineFragment);

        navigationBar.titleItems(tabText)
                .normalIconItems(normalIcon)
                .selectIconItems(selectIcon)
                .fragmentList(fragments)
                .centerImageRes(R.mipmap.healthy_button)
                .centerIconSize(60)
                //.centerTextStr(" ")
                .anim(null)
                .centerLayoutRule(EasyNavigationBar.RULE_BOTTOM)
                .centerLayoutBottomMargin(0)
                .centerAlignBottom(true)
                .fragmentManager(getSupportFragmentManager())
                .setOnTabClickListener(new EasyNavigationBar.OnTabClickListener() {
                    @Override
                    public boolean onTabSelectEvent(View view, int position) {
                        if (position == 0) {
                            ImmersionBar.with(MainActivity.this).transparentStatusBar().statusBarDarkFont(true).navigationBarColor(R.color.white).fitsSystemWindows(true).init();
                        } else if (position == 1) {
                            ImmersionBar.with(MainActivity.this).transparentStatusBar().statusBarDarkFont(true).navigationBarColor(R.color.white).fitsSystemWindows(true).init();
                        } else if (position == 2) {
                            ImmersionBar.with(MainActivity.this).transparentStatusBar().statusBarDarkFont(true).navigationBarColor(R.color.white).fitsSystemWindows(true).init();
                        } else if (position == 3) {
                            ImmersionBar.with(MainActivity.this)
                                    .transparentStatusBar()
                                    .statusBarDarkFont(false)
                                    .fitsSystemWindows(false)
                                    .init();
                            //ImmersionBar.with(MainActivity.this).transparentStatusBar().statusBarDarkFont(true).navigationBarColor(R.color.white).fitsSystemWindows(true).init();
                        }
                        return false;
                    }

                    @Override
                    public boolean onTabReSelectEvent(View view, int position) {
                        return false;
                    }

                })
                .setOnCenterTabClickListener(new EasyNavigationBar.OnCenterTabSelectListener() {
                    @Override
                    public boolean onCenterTabSelectEvent(View view) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {

                              //  ARouter.getInstance().build(GuidUrl.LOGIN).navigation();

                                showMunu();
                            }
                        });
                        return false;
                    }
                })

                .canScroll(true)
                .mode(EasyNavigationBar.NavigationMode.MODE_ADD)
                .build();

        navigationBar.setAddViewLayout(createWeiboView());
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
//        ImmersionBar.with(this)
//                .transparentStatusBar()
//                .statusBarDarkFont(false)
//                .fitsSystemWindows(false)
//                .navigationBarColor(R.color.mine_title)
//                .init();
    }

    private View createWeiboView() {
        ViewGroup view = (ViewGroup) View.inflate(MainActivity.this, R.layout.layout_add_view, null);
        menuLayout0 = view.findViewById(R.id.icon_group);
        menuLayout1 = view.findViewById(R.id.ll_add);
        cancelImageView = view.findViewById(R.id.cancel_iv);
        cancelImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeAnimation();
            }
        });
        for (int i = 0; i < 4; i++) {
            View itemView = View.inflate(MainActivity.this, R.layout.item_icon, null);
            ImageView menuImage = itemView.findViewById(R.id.menu_icon_iv);
            TextView menuText = itemView.findViewById(R.id.menu_text_tv);

            menuImage.setImageResource(menuIconItems0[i]);
            menuText.setText(menuTextItems0[i]);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.weight = 1;
            itemView.setLayoutParams(params);
            itemView.setVisibility(View.GONE);
            menuLayout1.addView(itemView);
        }
        for (int i = 0; i < 4; i++) {
            View itemView = View.inflate(MainActivity.this, R.layout.item_icon, null);
            ImageView menuImage = itemView.findViewById(R.id.menu_icon_iv);
            TextView menuText = itemView.findViewById(R.id.menu_text_tv);

            menuImage.setImageResource(menuIconItems1[i]);
            menuText.setText(menuTextItems1[i]);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.weight = 1;
            itemView.setLayoutParams(params);
            itemView.setVisibility(View.GONE);
            menuLayout0.addView(itemView);
        }

        return view;
    }

    private void showMunu() {
        startAnimation();
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                //＋ 旋转动画
                cancelImageView.animate().rotation(90).setDuration(400);
            }
        });
        //菜单项弹出动画
        for (int i = 0; i < menuLayout0.getChildCount(); i++) {
            final View child = menuLayout0.getChildAt(i);
            child.setVisibility(View.INVISIBLE);
            mHandler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    child.setVisibility(View.VISIBLE);
                    ValueAnimator fadeAnim = ObjectAnimator.ofFloat(child, "translationY", 600, 0);
                    fadeAnim.setDuration(500);
                    KickBackAnimator kickAnimator = new KickBackAnimator();
                    kickAnimator.setDuration(500);
                    fadeAnim.setEvaluator(kickAnimator);
                    fadeAnim.start();
                }
            }, i * 50 + 100);
        }

        for (int i = 0; i < menuLayout1.getChildCount(); i++) {
            final View child = menuLayout1.getChildAt(i);
            child.setVisibility(View.INVISIBLE);
            mHandler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    child.setVisibility(View.VISIBLE);
                    ValueAnimator fadeAnim = ObjectAnimator.ofFloat(child, "translationY", 600, 0);
                    fadeAnim.setDuration(500);
                    KickBackAnimator kickAnimator = new KickBackAnimator();
                    kickAnimator.setDuration(500);
                    fadeAnim.setEvaluator(kickAnimator);
                    fadeAnim.start();
                }
            }, i * 50 + 100);
        }

    }

    private void startAnimation() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //圆形扩展的动画
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                        int x = NavigationUtil.getScreenWidth(MainActivity.this) / 2;
                        int y = (int) (NavigationUtil.getScreenHeith(MainActivity.this) - NavigationUtil.dip2px(MainActivity.this, 25));
                        Animator animator = ViewAnimationUtils.createCircularReveal(navigationBar.getAddViewLayout(), x,
                                y, 0, navigationBar.getAddViewLayout().getHeight());
                        animator.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                                navigationBar.getAddViewLayout().setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {
                                //							layout.setVisibility(View.VISIBLE);
                            }
                        });
                        animator.setDuration(300);
                        animator.start();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * 关闭window动画
     */
    private void closeAnimation() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                cancelImageView.animate().rotation(0).setDuration(400);
            }
        });

        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

                int x = NavigationUtil.getScreenWidth(this) / 2;
                int y = (NavigationUtil.getScreenHeith(this) - NavigationUtil.dip2px(this, 25));
                Animator animator = ViewAnimationUtils.createCircularReveal(navigationBar.getAddViewLayout(), x,
                        y, navigationBar.getAddViewLayout().getHeight(), 0);
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        //							layout.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        navigationBar.getAddViewLayout().setVisibility(View.GONE);
                        //dismiss();
                    }
                });
                animator.setDuration(300);
                animator.start();
            }
        } catch (Exception e) {
        }
    }

}
